using System;
using System.Collections;
using System.Web.UI;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Context;
using PHSRAG.Insight21.Policy;
using PHSRAG.Utility;
using Utils = PHSRAG.Insight21.InsightUtilities;

/*Bhaskar- 01/18/2008
 * This page is used to show  for SQL reports for clinical trials
 * the actual implementation may be a little different
 */

namespace PHSRAG.Insight21.ClinicalTrials
{
    public partial class sqlReportHost : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string srptName = Request["rpt"]; //name of the report to serve up
                    string format = Request["fmt"]; //format of the report - PDF or Excel

                    if (!string.IsNullOrEmpty(srptName))
                    {
                        SSRSRequest.FetchAndDisplayReport(format, srptName, GetInputParameters(srptName));
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("There was a problem processing your request. Please contact helpdesk. <br>" + ex.Message);
                }
            }
        }

        # region InputParameters

        private Hashtable GetInputParameters(string srptName)
        {
            Hashtable tbl = new Hashtable();

            ICachePolicy cachePolicy = (ICachePolicy) Session[Names.CachePolicyKey];
            Budget budget = (Budget) cachePolicy.GetCachedObject(Budget.CacheKey);

            if (budget != null)
            {
                tbl.Add("BudgetId", budget.Id.ToString());
            }
            else
            {
                throw new Exception("Budget Id Not Specified. Please contact the Administrator");
            }

            //User Information
            UserContext userContext = (UserContext) Session[UserContext.CacheKey];

            tbl.Add(SSRSRequest.ReportParameterKeys.UserName, userContext.LastName + "," + userContext.FirstName); //username
            tbl.Add(SSRSRequest.ReportParameterKeys.WebUrl, Settings.GetKeyValue("weburl", "http://insight2.partners.org")); //Domain User Id

            return tbl;
        }

        #endregion
    }
}